﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebApplication5.Model;

namespace WebApplication5.Controller
{
    public class loginCtr
    {
        dmaa0912DannyEntities2 db = new dmaa0912DannyEntities2();



        public Login getLogin(int id)
        {
            return (from l in db.Login
                    where l.id == id
                    select l).FirstOrDefault();
        }

        public Login GetLoginByPhoneNumber(int phoneNr)
        {
            return (from l in db.Login
                    where l.phoneNumber == phoneNr
                    select l).FirstOrDefault();
        }


        public void deleteLoginByPhoneNumber(int phoneNr)
        {
            Login log = (from v in db.Login
                         where v.phoneNumber == phoneNr
                         select v).FirstOrDefault();

            db.Login.Remove(log);

            db.SaveChanges();
        }

        public void deleteLogin(int id)
        {
            Login l = (from v in db.Login
                       where v.id == id
                       select v).FirstOrDefault();

            db.Login.Remove(l);

            db.SaveChanges();
        }


        public void VerifyLogin(int verifyId)
        {
            Login l = (from v in db.Login
                       where v.id == verifyId
                       select v).FirstOrDefault();

            l.verfied = true;

            db.SaveChanges();
        }

        public void insertLogin(Login u)
        {
            validationOfLogin(u);

            db.Login.Add(u);
            db.SaveChanges();
        }

        public void editLogin(Login lo)
        {
            validationOfLogin(lo);

            Login login = db.Login.FirstOrDefault(l => l.phoneNumber == lo.phoneNumber);
            login.lastName = lo.lastName;
            login.password = lo.password;
            login.userName = lo.userName;
            login.firstName = lo.firstName;
            login.password = lo.password;
            login.Project = lo.Project;
            login.companyId = lo.companyId;
            login.phoneNumber = lo.phoneNumber;
            login.verfied = true;

            db.SaveChanges();
        }


        private void validationOfLogin(Login l)
        {

            Regex onlyDigits = new Regex("^[0-9]+$");
            Regex onlyLetters = new Regex(@"^[a-zA-Z]+$");
            Regex onlyLettersAndNumbers = new Regex(@"[a-zæøå 0-9]+");

            if (string.IsNullOrEmpty(l.firstName) || (!onlyLetters.IsMatch(l.firstName)))
                throw new Exception("Fornavn er ikke korrekt kun bogstaver tilladt");

            if ((string.IsNullOrEmpty(l.lastName)) || (!onlyLetters.IsMatch(l.lastName)))
                throw new Exception("Navn er ikke korrekt kun bogstaver tilladt");

            if (!onlyDigits.IsMatch(l.phoneNumber.ToString()))
                throw new Exception("indtast et korrekt telefon nummer");

            if (!onlyDigits.IsMatch(l.companyId.ToString()))
                throw new Exception("indtast et korrekt company nummer");

            if (string.IsNullOrEmpty(l.userName))
                throw new Exception("Indtast et brugernavn");

            if (string.IsNullOrEmpty(l.password))
                throw new Exception("Indtast et kodeord");
        }
    }
}